package org.sxp.modules.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.sxp.common.annotation.SysLog;
import org.sxp.common.exception.AppException;
import org.sxp.common.utils.PageInfo;
import org.sxp.common.utils.PageResult;
import org.sxp.common.validator.Assert;
import org.sxp.common.validator.ValidatorUtils;
import org.sxp.common.validator.group.AddGroup;
import org.sxp.common.validator.group.UpdateGroup;
import org.sxp.common.web.WrapperResponse;
import org.sxp.modules.sys.entity.SysUserEntity;
import org.sxp.modules.sys.service.SysDictService;
import org.sxp.modules.sys.service.SysUserRoleService;
import org.sxp.modules.sys.service.SysUserService;
import org.sxp.modules.sys.shiro.ShiroUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 系统用户
 *
 *
 */
@RestController
@RequestMapping(value = "/sys/user")
@Api(description="User", tags = "系统用户")
@SysLog(modules = "sys", value = "系统用户")
public class SysUserController extends AbstractController {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserRoleService sysUserRoleService;
	@Autowired
	private SysDictService dictService;

	/**
	 * 列表
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name="pageNum", value = "当前页码", dataType="int", paramType = "header"),
			@ApiImplicitParam(name="pageSize",value="每页条目",dataType="int", paramType = "header"),
			@ApiImplicitParam(name="orderField",value="排序字段",dataType="string", paramType = "header"),
			@ApiImplicitParam(name="orderType",value="排序类型",dataType="string", paramType = "header", example = "asc或者desc")
	})
	@RequestMapping(value = "/list",method = {RequestMethod.POST,RequestMethod.GET})
	@ApiOperation(value = "查询列表")
	public WrapperResponse<List<SysUserEntity>> list(SysUserEntity params) throws Exception{
		//请求头获取分页参数
		PageInfo pageInfo = buildPageInfo();
		PageResult page = sysUserService.queryPage(
				pageInfo
				,new LambdaQueryWrapper<SysUserEntity>());

		return (WrapperResponse)WrapperResponse.success(page);
	}
	
	/**
	 * 获取登录的用户信息
	 */
	@RequestMapping(value = "/info",method = RequestMethod.GET)
	public WrapperResponse<SysUserEntity> info(){
		return WrapperResponse.success(getUser());
	}
	
	/**
	 * 修改登录用户密码
	 */
	@SysLog("修改密码")
	@RequestMapping(value = "/password",method = RequestMethod.POST)
	@ApiOperation(value = "修改密码")
	public WrapperResponse password(String password, String newPassword){
		Assert.isBlank(newPassword, "新密码不为能空");

		//原密码
		password = ShiroUtils.sha256(password, getUser().getSalt());
		//新密码
		newPassword = ShiroUtils.sha256(newPassword, getUser().getSalt());
				
		//更新密码
		boolean flag = sysUserService.updatePassword(getUserId(), password, newPassword);
		if(!flag){
			throw new AppException("原密码不正确");
		}
		
		return WrapperResponse.success(null);
	}
	
	/**
	 * 用户信息
	 */
	@RequestMapping(value = "/info/{userId}",method = RequestMethod.GET)
	@RequiresPermissions("sys:user:info")
	@ApiOperation(value = "用户信息")
	public WrapperResponse<SysUserEntity> info(@PathVariable("userId") Long userId){
		SysUserEntity user = sysUserService.getById(userId);
		
		//获取用户所属的角色列表
		List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
		user.setRoleIdList(roleIdList);

		return (WrapperResponse)WrapperResponse.success(user);
	}
	
	/**
	 * 保存用户
	 */
	@SysLog("保存用户")
	@RequestMapping(value = "/save",method = RequestMethod.POST)
	@RequiresPermissions("sys:user:save")
	@ApiOperation(value = "保存用户")
	public WrapperResponse<SysUserEntity> save(@RequestBody SysUserEntity user){
		ValidatorUtils.validateEntity(user, AddGroup.class);
		
		sysUserService.saveUser(user);

		return (WrapperResponse)WrapperResponse.success(user);
	}
	
	/**
	 * 修改用户
	 */
	@SysLog("修改用户")
	@RequestMapping(value = "/update",method = RequestMethod.POST)
	@RequiresPermissions("sys:user:update")
	@ApiOperation(value = "修改用户")
	public WrapperResponse<SysUserEntity> update(@RequestBody SysUserEntity user){
		ValidatorUtils.validateEntity(user, UpdateGroup.class);

		sysUserService.update(user);

		return (WrapperResponse)WrapperResponse.success(null);
	}
	
	/**
	 * 删除用户
	 */
	@SysLog("删除用户")
	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	@RequiresPermissions("sys:user:delete")
	@ApiOperation(value = "删除用户")
	public WrapperResponse delete(@RequestBody Long[] userIds){
		if(ArrayUtils.contains(userIds, 1L)){
			throw new AppException("系统管理员不能删除");
		}
		
		if(ArrayUtils.contains(userIds, getUserId())){
			throw new AppException("当前用户不能删除");
		}

		sysUserService.removeByIds(Arrays.asList(userIds));

		return WrapperResponse.success(null);
	}
}
